# -*- coding: utf-8 -*-
# @Time    : 2024/11/8  8:57
# @Author  : Nabby
# @FileName: dict_api.py
# @Desc    :

from app import app, request
from application.dict_manage import DictService
from infrastructure.utils.http import responser


@app.route("/api/get_dict_type_list", endpoint="get_dict_type_list", methods=["POST"])
def get_dict_type_list():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictTypeName",
        ],
    )
    if res_status == "success":
        return DictService.get_dict_type_list(
            name=rjson.get("dictTypeName"),
            page=rjson.get("page", 1),
            page_size=rjson.get("pageSize", 999),
        )
    return rjson


@app.route("/api/add_dict_type", endpoint="add_dict_type", methods=["POST"])
def add_dict_type():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictTypeName",
        ],
    )
    if res_status == "success":
        return DictService.add_dict_type(
            name=rjson.get("dictTypeName"), remark=rjson.get("remark")
        )
    return rjson


@app.route("/api/update_dict_type", endpoint="update_dict_type", methods=["POST"])
def update_dict_type():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictTypeId",
            "dictTypeName",
        ],
    )
    if res_status == "success":
        return DictService.update_dict_type(
            dict_type_id=rjson.get("dictTypeId"),
            name=rjson.get("dictTypeName"),
        )
    return rjson


@app.route("/api/delete_dict_type", endpoint="delete_dict_type", methods=["POST"])
def delete_dict_type():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictTypeId",
        ],
    )
    if res_status == "success":
        return DictService.delete_dict_type(
            dict_type_id=rjson.get("dictTypeId")
        )
    return rjson


@app.route("/api/get_dict_data_list", endpoint="get_dict_data_list", methods=["POST"])
def get_dict_data_list():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictTypeId",
        ],
    )
    if res_status == "success":
        return DictService.get_dict_data_list(
            dict_type_id=rjson.get("dictTypeId"),
            page=rjson.get("page"),
            page_size=rjson.get("pageSize"),
        )
    return rjson


@app.route("/api/add_dict_data", endpoint="add_dict_data", methods=["POST"])
def add_dict_data():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictLabel",
            "dictValue",
            "dictTypeId",
            "dictSort",
        ],
    )
    if res_status == "success":
        return DictService.add_dict_data(
            dict_label=rjson.get("dictLabel"),
            dict_value=rjson.get("dictValue"),
            dict_type_id=rjson.get("dictTypeId"),
            dict_sort=rjson.get("dictSort"),
            remark=rjson.get("remark"),
        )
    return rjson


@app.route("/api/update_dict_data", endpoint="update_dict_data", methods=["POST"])
def update_dict_data():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictDataId",
            "dictLabel",
            "dictValue",
            "dictTypeId",
            "dictSort",
        ],
    )
    if res_status == "success":
        return DictService.update_dict_data(
            dict_data_id=rjson.get("dictDataId"),
            dict_label=rjson.get("dictLabel"),
            dict_value=rjson.get("dictValue"),
            dict_type_id=rjson.get("dictTypeId"),
            dict_sort=rjson.get("dictSort"),
            remark=rjson.get("remark"),
        )
    return rjson


@app.route(
    "/api/update_dict_data_status", endpoint="update_dict_data_status", methods=["POST"]
)
def update_dict_data_status():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictDataId",
            "status",
        ],
    )
    if res_status == "success":
        return DictService.update_dict_data_status(
            dict_data_id=rjson.get("dictDataId"), status=rjson.get("status")
        )
    return rjson


@app.route("/api/delete_dict_data", endpoint="delete_dict_data", methods=["POST"])
def delete_dict_data():
    res_status, rjson = responser.post_param_check(
        request,
        [
            "dictDataId",
        ],
    )
    if res_status == "success":
        return DictService.delete_dict_data(
            dict_data_id=rjson.get("dictDataId")
        )
    return rjson
